<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组对象去重</title>
</head>
<body>
  <script>
    let arrObj=[{a:1,b:2},{a:2,b:3},{a:1,b:4}];
    //第一种
    // function unique(arr){
    //   let obj={},result=[];
    //   for(var i=0;i<arr.length;i++){
    //     if(!obj[arr[i]["a"]]){
    //       result.push(arr[i]);
    //       obj[arr[i]["a"]]=true
    //     }
    //   }
    //   return result;  
    // }
    // console.log(unique(arrObj))   //[{a: 1, b: 2},{a: 2, b: 3}]
    
    //第二种
    function unique(arr){
      let obj={};
      arr=arr.reduce((v,next)=>{
        obj[next.a]?"":obj[next.a]=true&&v.push(next)
        return v;
      },[])
      return arr;
    }
    console.log(unique(arrObj))
  </script>
</body>
</html>